package com.kiln.juxingqs.ui.vm

import android.app.Application
import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import client.global.commlib.network.data.JsonResult
import client.global.commlib.network.data.Ret
import client.global.commlib.out_time
import client.global.commlib.toast.toast
import client.global.commlib.utils.StoreUtil
import com.kiln.juxingqs.api.bean.GetDddBatchRet
import com.kiln.juxingqs.api.bean.QueryUser
import com.kiln.juxingqs.api.bean.UserInfo
import com.kiln.juxingqs.ui.viewmodel.MiddleVm
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import retrofit2.Response

class ZrWtViewModel(val app: Application) : MiddleVm(app) {
    val call = MutableLiveData<JsonResult<Ret>>()
    val queryRet = MutableLiveData<JsonResult<QueryUser>>()
    val isShowing = MutableLiveData<Boolean>()

    val sNum = MutableLiveData<String>("")
    val otherAccount = MutableLiveData<String>("")
    val password = MutableLiveData<String>("")
    val otherName = MutableLiveData<String>("")
    val selectDddBath = MutableLiveData<GetDddBatchRet.Batch>()

    val userInfo = MutableLiveData<UserInfo>().apply {
        value = StoreUtil.getBean<UserInfo>(app)
    }

    val wtType = MutableLiveData<Int>(1)

    fun wTTypeChange(vale: Int) {
        wtType.value = vale
    }

    /**
     */
    fun ok() {
//        if (wtType.value!! < 0
//        ) {
////            toast("请选择委托类型")
////            return
//        }


        if (TextUtils.isEmpty(otherAccount.value)
        ) {
            toast("请输入对方账号")
            return
        }
        if (TextUtils.isEmpty(otherName.value)
        ) {
            toast("对方姓名不能为空")
            return
        }
        if (TextUtils.isEmpty(sNum.value)
        ) {
            toast("请输入交易数量")
            return
        }
        if (TextUtils.isEmpty(password.value)
        ) {
            toast("请输入交易密码")
            return
        }

        isShowing.value = true
//        viewModelScope.launch {
//            flow {
//                var emm: Response<JsonResult<Ret>>? = null
//                out_time {
//                    val ret = server.validatePayPwd(password.value!!)
//                    emm = ret
//                }
//                emit(emm)
//            }.map {
//                var emm: Response<JsonResult<Ret>>? = it
//                it?.body()?.let {
//                    if (it.isSuccess) {
//                        var payType = "1"
//                        out_time {
//                            val ret = server.seller_transfer_batch(
//                                otherAccount.value!!,
//                                sNum.value!!,
//                                "1",//""!!
//                                "${selectDddBath.value?.id}",
//                                "${selectDddBath.value?.goods_id}",
//                                ""
//                            )
//                            emm = ret
//                        }
//                    }
//                }
//                emm
//            }.flowOn(Dispatchers.IO)
//                .onCompletion {
//                    isShowing.value = false
//                }.catch { cause ->
//                    if (cause != null) {
//                        toast(cause.message)
//                    }
//                }.collectLatest {
//                    call.value = it?.body()
//
//                }
//        }
    }

    fun query() {
        if (otherAccount.value.isNullOrEmpty()) {
            toast("请输入内容")
            return
        }
        hold({
            server.queryUser(otherAccount.value!!)
        }, {
            isShowing.value = true
        }, {
            queryRet.value = it
        }, complete = {
            isShowing.value = false
        })
    }
}